//
//  DescriptionView.m
//  YiShangPin
//
//  Created by 衣尚品1 on 15/3/16.
//  Copyright (c) 2015年 yishangpin2. All rights reserved.
//

#import "DescriptionView.h"
#import "AppDelegate.h"

@implementation DescriptionView{
    AppDelegate *delegate;
    CGContextRef context;
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    //创建画布
    context = UIGraphicsGetCurrentContext();
    //设置线条颜色
    [[UIColor grayColor] set];
    //设置线条宽度
    CGContextSetLineWidth(context, 0.3f);
    
    delegate.isHaveHeight = NO;
    [self addView1];
    
    //画线
    CGContextStrokePath(context);
}

- (float)addView1{
    
    delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    UIFont *buttonFont = [UIFont systemFontOfSize:15.0f];
    UIFont *lableFont = [UIFont fontWithName:@"TimesNewRomanPS-ItalicMT" size:16];
    
    UILabel *lable1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 350, 50)];
//    lable1.text = @"Black Chiffon Women Fashion Long Sleeve Round Collar Korean Style Batwing T-Shirt S/M/L/XL FLC3073-8021-15-Black-403-3";
//    lable1.text = @"Men's Short Sleeve Cotton Polo Shirts";
    lable1.text = delegate.detailsDic[@"title"];
    
    lable1.font = lableFont;
    //设置lable显示的行数，0为不限制
    lable1.numberOfLines = 0;
    //lable根据单词分行
    lable1.lineBreakMode = NSLineBreakByWordWrapping;
    //根据字符个数获取lable的尺寸，这里的size是设置lable自适应的最大宽度和高度
    CGSize size1 = [lable1.text boundingRectWithSize:CGSizeMake(delegate.width-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:lable1.font,NSFontAttributeName, nil] context:nil].size;
    //根据获得的size重新设置lable的frame
    lable1.frame = CGRectMake(20, 20, delegate.width-40, size1.height);

    
    UILabel *lable2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, delegate.width-20, 50)];
//    lable2.text = @"¥169";

    lable2.text = [NSString stringWithFormat:@"$%@",delegate.detailsDic[@"newsprice"]];
    lable2.font = [UIFont fontWithName:@"Arial" size:25];
    lable2.numberOfLines = 0;
    lable2.lineBreakMode = NSLineBreakByWordWrapping;
    CGSize size2 = [lable2.text boundingRectWithSize:CGSizeMake(lable2.frame.size.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:lable2.font,NSFontAttributeName, nil] context:nil].size;
    lable2.frame = CGRectMake(30, size1.height+45, size2.width, size2.height);
    //    lable1.backgroundColor = [UIColor redColor];
    
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(delegate.width-100, size1.height+40, 70, size2.height+10)];
    button.backgroundColor = [UIColor orangeColor];
    [button setTitle:@"Buy" forState:0];
    button.layer.cornerRadius = 8.0f;
    
    //button的点击事件
    [button addTarget:self action:@selector(clickBuyButton) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *lable3 = [[UILabel alloc] initWithFrame:CGRectMake(20, size1.height+70+size2.height, 100, 18)];
    lable3.text = @"Description";
    lable3.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldItalicMT" size:18];;
    
    
    UILabel *lable4 = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, delegate.width-80, 30)];
//    lable4.text = @"Show Color BlackFabric Chiffon Available Size:S/M/L/XlBust:110cm Length:78cm Waist:68cmBust:112cm Length:80cm Waist:70cmBust:114cm Lenght:82cm Waist:72cmBust:116cm Length:84cm Waist:74cm";
    lable4.text = delegate.detailsDic[@"description"];
    
    lable4.font = lableFont;
    lable4.numberOfLines = 3;
    CGSize size4 = [lable4.text boundingRectWithSize:CGSizeMake(delegate.width-80, 80) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:lable4.font,NSFontAttributeName, nil] context:nil].size;
    lable4.frame = CGRectMake(30, lable3.frame.size.height+size1.height+70+size2.height, delegate.width-60, size4.height);
    
    
    UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(delegate.width-190, lable3.frame.size.height+size1.height+70+size2.height+size4.height, 90, 17)];
    [button1 setTitle:@"Sizing Chart" forState:0];
    button1.titleLabel.font = buttonFont;
    [button1 setTitleColor:[UIColor colorWithRed:47.f/255 green:164.f/255 blue:230.f/255 alpha:1] forState:0];
    
    UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(delegate.width-90, lable3.frame.size.height+size1.height+70+size2.height+size4.height, 90, 17)];
    [button2 setTitle:@"Read more..." forState:0];
    button2.titleLabel.font = buttonFont;
    [button2 setTitleColor:[UIColor colorWithRed:47.f/255 green:164.f/255 blue:230.f/255 alpha:1] forState:0];
    [button2 addTarget:self action:@selector(clickReadMore) forControlEvents:UIControlEventTouchUpInside];
    
    //设置每条线的起始点
    CGPoint view1Points1[] = {
        CGPointMake(0, size1.height+30),
        CGPointMake(50, size1.height+30),
        CGPointMake(55, size1.height+38),
        CGPointMake(60, size1.height+30),
        CGPointMake(delegate.width, size1.height+30),
    };
    CGContextAddLines(context, view1Points1, 5);
    
    CGPoint view1Points2[] = {
        CGPointMake(0, size1.height+60+size2.height),
        CGPointMake(delegate.width, size1.height+60+size2.height),
    };
    CGContextAddLines(context, view1Points2, 2);
    
    CGPoint view1Points3[] = {
        CGPointMake(delegate.width-95, lable3.frame.size.height+size1.height+70+size2.height+size4.height),
        CGPointMake(delegate.width-95, lable3.frame.size.height+size1.height+87+size2.height+size4.height),
    };
    CGContextAddLines(context, view1Points3, 2);
    
    CGPoint view1Points4[] = {
        CGPointMake(0, lable3.frame.size.height+size1.height+97+size2.height+size4.height),
        CGPointMake(delegate.width, lable3.frame.size.height+size1.height+97+size2.height+size4.height),
    };
    CGContextAddLines(context, view1Points4, 2);
    
    if (delegate.isHaveHeight == NO) {
        [self addSubview:lable1];
        [self addSubview:lable2];
        [self addSubview:button];
        [self addSubview:lable3];
        [self addSubview:lable4];
        [self addSubview:button1];
        [self addSubview:button2];
        
    }
    
    
    
    float viewHeight = lable3.frame.size.height+size1.height+107+size2.height+size4.height;
    
    return viewHeight;

}

- (void)clickBuyButton{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"clickBuyButton" object:nil];
}

- (void)clickReadMore{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"clickReadMore" object:@"ReadMore"];
}

@end
